<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>图书列表</title>
    <style>
        :root {
            --sidebar-width: 200px;
            --primary-color: #2c3e50;
        }

        .page-container {
            padding: 20px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .table-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 14px;
        }

        .data-table th {

            color: #333;
            padding: 12px 15px;
            text-align: left;
        }

        .data-table td {
            padding: 12px 15px;
            border-bottom: 1px solid #eee;
            color: #5a6268;
        }

        .data-table tbody tr:hover {
            background: #f8f9fa;
        }

        .btn-primary {
            background: #28a745;
            color: white;
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background 0.3s;
        }

        .btn-primary:hover {
            background: #218838;
        }

        .pagination {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-top: 20px;
        }

        .btn-pagination {
            background: #fff;
            color: #333;
            padding: 6px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
        }

        .btn-pagination:hover {
            background: #eee;
        }

        .page-info {
            color: #666;
        }

        /* 操作按钮样式 */
        .btn-edit {
            background: #007bff;
            color: white;
            padding: 4px 8px;
            margin-right: 5px;
        }

        .btn-delete {
            background: #dc3545;
            color: white;
            padding: 4px 8px;
        }

        button {
            border-radius: 3px;
            transition: all 0.3s;
            border: none;
        }
    </style>

    <style>
        /* 新增表单样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            justify-content: center;
            align-items: center;
        }

        .modal-content {
            background: white;
            padding: 25px;
            border-radius: 8px;
            width: 400px;
        }

        .two-column-form {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
            margin-top: 20px;
        }

        .form-group {
            display: flex;
            flex-direction: column;
            gap: 5px;
        }

        .form-group label {
            font-weight: 500;
            color: #495057;
        }

        .form-group input {
            padding: 8px;
            border: 1px solid #ced4da;
            border-radius: 4px;
        }

        @media (max-width: 600px) {
            .two-column-form {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
<!-- 分页浏览页面 -->
<div class="page-container">
    <div class="table-header">
        <h3>图书列表</h3>

    </div>

    <table class="data-table" id="bookTable">
        <thead>
        <tr>
            <th>ISBN</th>
            <th>书名</th>
            <th>作者</th>
            <th>出版社</th>
            <th>出版日期</th>
            <th>库存</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tableBody">
        <tr>
            <td>978-7-302-12345-6</td>
            <td>计算机科学导论</td>
            <td>张三</td>
            <td>15</td>
            <td>
                <button class="btn-edit">编辑</button>
                <button class="btn-delete">删除</button>
            </td>
        </tr>
        <!-- 更多数据行... -->
        </tbody>
    </table>

    <div class="pagination">
        <button class="btn-pagination" onclick="PageChange(false)">上一页</button>
        <span class="page-info" id="pageDiv">第1页 / 共5页</span>
        <button class="btn-pagination" onclick="PageChange(true)">下一页</button>
    </div>

    <!-- 编辑弹窗 -->
    <div class="modal" id="editModal">
        <div class="modal-content">
            <h3>图书信息</h3>
            <form id="bookForm" onsubmit="handleSubmit(event)">
                <input type="hidden" id="book_id">
                <div class="two-column-form">
                    <div class="form-group">
                        <label>ISBN：</label>
                        <input type="text" id="isbn" required>
                    </div>
                    <div class="form-group">
                        <label>书名：</label>
                        <input type="text" id="title" required>
                    </div>
                    <div class="form-group">
                        <label>作者：</label>
                        <input type="text" id="author" required>
                    </div>
                    <div class="form-group">
                        <label>出版社：</label>
                        <input type="text" id="publisher">
                    </div>
                    <div class="form-group">
                        <label>出版日期：</label>
                        <input type="date" id="publishDate">
                    </div>
                    <div class="form-group">
                        <label>库存数量：</label>
                        <input type="number" id="stock" min="0" required>
                    </div>
                    <div class="form-actions">
                        <button type="submit" class="btn-edit" style="padding: 6px 12px;" >保存</button>
                        <button type="button" class="btn-delete" style="padding: 6px 12px;" onclick="closeModal()">取消</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="myajax.js"></script>
<script>

    var currData = null

    getData()

    function PageChange(isNext = true) {
        if (currData == null){
            alert("页面未加载完全")
            return
        }
        if (isNext){
            if (currData.current_page >= currData.total_pages){
                alert("已经是最后一页了")

            }else{
                currData.current_page = currData.current_page + 1
                getData()
            }
        }else{
            if (currData.current_page <= 1 ){
                alert("已经是第一页了")

            }else{
                currData.current_page = currData.current_page - 1
                getData()
            }
        }
    }

    //删除书籍
    function deleteBook(index) {
        const token = sessionStorage.getItem('token');
        if (token) {

        }else{
            if (confirm('还未登录，是否确定要去登录页面？')){
                window.parent.location.href = '/static/login.html';
            }
            return
        }

        if (confirm('确定要删除吗？')) {
            ajaxRequest( {
                method: 'delete',
                url:`/jwt/bookDel?book_id=${index}`,

            }).then(data => {
                if(data.status == 0) {
                    getData();
                }else{
                    alert(data.status + '：' + data.msg)
                }
            }).catch(error => alert('Error:'+ error.msg));
        }
    }

    //提交修改
    function handleSubmit(e) {
        e.preventDefault();

        const newBook = {
            book_id: parseInt(document.getElementById('book_id').value),
            isbn: document.getElementById('isbn').value,
            title: document.getElementById('title').value,
            author: document.getElementById('author').value,
            publisher: document.getElementById('publisher').value,
            publish_date: document.getElementById('publishDate').value,
            stock: parseInt(document.getElementById('stock').value)
        };
        // console.log(newBook)
        // return

        ajaxRequest( {
            method: 'put',
            url:"/jwt/bookUpdate",
            data:newBook

        }).then(data => {
            if(data.status == 0) {

                getData()
                closeModal();
            }else{
                alert(data.status + '：' + data.msg)
            }
        }).catch(error => alert('Error:'+ error.msg));

    }

    //获取数据
    function getData(){
        let url = "/books"
        if (currData)  {
            url = "/books?page="+currData.current_page
        }
        ajaxRequest( {
            method: 'get',
            url:url,

        }).then(data => {
            if(data.status == 0) {
                currData = data
                renderTable()
            }else{
                alert(data.status + '：' + data.msg)
            }
        }).catch(error => alert('Error:'+ error.msg));
    }

    function renderTable() {
        const tbody = document.getElementById('tableBody');
        tbody.innerHTML = currData.data.map((book, index) => `
                <tr>
                    <td>${book.isbn}</td>
                    <td>${book.title}</td>
                    <td>${book.author}</td>
                    <td>${book.publisher || '-'}</td>
                    <td>${book.publish_date || '未录入'}</td>
                    <td>${book.stock}</td>
                    <td class="action-buttons">
                        <button class="btn-edit" onclick="editBook(${index})">编辑</button>
                        <button class="btn-delete" onclick="deleteBook(${book.book_id})">删除</button>
                    </td>
                </tr>
        `).join('');
        //更新页码信息
        document.getElementById("pageDiv").innerText = `第${currData.current_page}页 / 共${currData.total_pages}页`

    }

    function editBook(index) {
        const token = sessionStorage.getItem('token');
        if (token) {

        }else{
            if (confirm('还未登录，是否确定要去登录页面？')){
                window.parent.location.href = '/static/login.html';
            }
            return
        }

        const book = currData.data[index];

        showForm(book);
    }

    function showForm(book = {}) {
        // 修改为直接使用book.index
        document.getElementById('book_id').value = book.book_id ?? '0';
        document.getElementById('isbn').value = book.isbn || '';
        document.getElementById('title').value = book.title || '';
        document.getElementById('author').value = book.author || '';
        document.getElementById('publisher').value = book.publisher || '';
        document.getElementById('publishDate').value = book.publish_date || '';
        document.getElementById('stock').value = book.stock || '';
        document.getElementById('editModal').style.display = 'flex';
    }

    function closeModal() {
        document.getElementById('editModal').style.display = 'none';
        document.getElementById('bookForm').reset();
    }
</script>
</body>
</html>


